package com.tbsoft.ehr.login;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.tbsoft.ehr.model.BaseEntity;
/**
 * @desc 菜单信息
 * @author vastdream
 *
 */
@Entity
@Table(name="EHR_MENU")
public class MenuInfo extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String menuId;
	private String menuUrl;
	private String target;
	private String rel;
	private String menuDesc;
	private int level;
	private String parentMenuId;
	
	private boolean select=false;
	@Transient
	public boolean isSelect() {
		return select;
	}
	public void setSelect(boolean select) {
		this.select = select;
	}
	private Set<MenuInfo> childMenuInfo = new HashSet<MenuInfo>();
		
	private Set<RoleInfo> roleSet = new HashSet<RoleInfo>();
	@Id
	@Column(name="MENU_ID",length=10)
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	@Column(name="MENU_URL",length=100)
	public String getMenuUrl() {
		return menuUrl;
	}
	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}
	@Column(name="MENU_TARGET",length=20)
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	@Column(name="MENU_REL",length=100)
	public String getRel() {
		return rel;
	}
	public void setRel(String rel) {
		this.rel = rel;
	}
	@Column(name="MENU_DESC",length=100)
	public String getMenuDesc() {
		return menuDesc;
	}
	public void setMenuDesc(String menuDesc) {
		this.menuDesc = menuDesc;
	}
	
	@Column(name="MENU_LEVLE")
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	@Column(name="MENU_PARENTID",length=10)
	public String getParentMenuId() {
		return parentMenuId;
	}
	public void setParentMenuId(String parentMenuId) {
		this.parentMenuId = parentMenuId;
	}
	
	@ManyToMany(mappedBy="menuSet",fetch=FetchType.EAGER,cascade={CascadeType.MERGE})
	public Set<RoleInfo> getRoleSet() {
		return roleSet;
	}
	public void setRoleSet(Set<RoleInfo> roleSet) {
		this.roleSet = roleSet;
	}
	
	@Transient
	public Set<MenuInfo> getChildMenuInfo() {
		return childMenuInfo;
	}
	public void setChildMenuInfo(Set<MenuInfo> childMenuInfo) {
		this.childMenuInfo = childMenuInfo;
	}
	@Transient
	public synchronized List<MenuInfo> getChildMenuList() {
		List<MenuInfo> tmpList = new ArrayList<MenuInfo>();
		if(childMenuInfo.size()>0){
			tmpList.addAll(childMenuInfo);
		}
		Collections.sort(tmpList, new Comparator<MenuInfo>(){

			@Override
			public int compare(MenuInfo o1, MenuInfo o2) {
				return o1.getMenuId().compareTo(o2.getMenuId());
			}}   );
		return tmpList;
	}
	@Override
	public int hashCode() {
		
			return (""+menuId).hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null){
			return false;
		}
		if(obj instanceof MenuInfo)
			return (""+menuId).equals(((MenuInfo)obj).getMenuId());
		else
			return false;
	}
	@Override
	public String toString() {
		return menuId+":"+menuUrl+":"+menuDesc;
	}
}
